package LeetCode.leetcode34;

class Solution {
    public int[] searchRange(int[] nums, int target) {
        int min = findMinNumIndex(nums,target);
        int max = findMinNumIndex(nums,target+1) - 1;
        if (min == nums.length || nums[min] != target)
            return new int[]{-1,-1};
        return  new int[]{min,max};
    }
    public int findMinNumIndex(int[] nums, int target) {
        int l = 0, r = nums.length - 1;
        while (l <= r) {
            int mid = (r - l) / 2 + l;
            if (target > nums[mid]) {
                l = mid + 1;
            } else {
                r = mid - 1;
            }
        }
        return l;
    }
    public int findMaxNumIndex(int[] nums, int target) {
        int l = 0, r = nums.length - 1;
        while (l <= r) {
            int mid = (r - l) / 2 + l;
            if (target < nums[mid]) {
                r = mid -1;
            } else {
                l = mid + 1;
            }
        }
        return r;
    }
}